package com.xt.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.xt.website.dao.CategoryDao;
import com.xt.website.dao.ProductDao;
import com.xt.website.po.*;
import com.xt.website.service.ProductService;
import com.xt.website.util.MyPageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: jiangyu
 * @Date: 2019/8/18 14:53
 * @Description:
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;
    @Resource
    private CategoryDao categoryDao;

    /**
     * 根据一级类目，查询的产品
     */
    @Override
    public MyPageInfo<Product> findProductsByCid(Integer pageNow, Integer cid, Integer pageSize) {
        //查询当前类目CategoryExt
        CategoryExt<CategorySecond> categoryExt = categoryDao.findCategoryByCid(cid);
        if(pageSize == null){
            pageSize = 9;
        }
        PageHelper.startPage(pageNow, pageSize);
        List<Product> list = productDao.findProductsByCid(cid);
        MyPageInfo<Product> page = new MyPageInfo<Product>(list,5);
        //将CategoryExt封装进分页实体中
        page.setCategoryExt(categoryExt);
        System.out.println("总数量：" + page.getTotal());
        System.out.println("当前页查询记录：" + page.getList().size());
        System.out.println("当前页码：" + page.getPageNum());
        System.out.println("每页显示数量：" + page.getPageSize());
        System.out.println("总页：" + page.getPages());
        return page;
    }

    /**
     * 根据二级类目，查询的产品
     */
    @Override
    public MyPageInfo<Product> findProductsByCsid(Integer pageNow,Integer csid) {
        //查询当前二级类目详情
        CategorySecond categorySecond = categoryDao.findCategorySecondByCsid(csid);
        PageHelper.startPage(pageNow, 9);
        List<Product> list = productDao.findProductsByCsid(csid);
        MyPageInfo<Product> page = new MyPageInfo<Product>(list,5);
        //将CategorySecond封装进分页实体中
        page.setCategorySecond(categorySecond);
        System.out.println("总数量：" + page.getTotal());
        System.out.println("当前页查询记录：" + page.getList().size());
        System.out.println("当前页码：" + page.getPageNum());
        System.out.println("每页显示数量：" + page.getPageSize());
        System.out.println("总页：" + page.getPages());
        return page;
    }

    /**
     * 查询产品详情
     */
    @Override
    public ProductExt<ProductFile> productDetails(Integer pid) {
        return productDao.productDetails(pid);
    }
}
